/****************************************************
	文件：GameRoot.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/31 16:10:35
	功能：游戏入口
*****************************************************/

using System.Collections;
using UnityEngine;
using HTUtility;

namespace AIModule.Utility.Example
{
    public class GameRoot : MonoSingleton<GameRoot>
    {
        public System.Random Random;
        public int Seed;

        private PlayerEntity mPlayer;
        private EnemyEntity mEnemy;
        /// <summary>
        /// 回合数
        /// </summary>
        private int mCount = 0;

        /// <summary>
        /// 游戏结束的标志位
        /// </summary>
        public ReactiveProperty<bool> GameOver = new ReactiveProperty<bool>(false);

        private void Start()
        {
            Seed = MathUtil.GetRandom(1, 100);
            Random = new System.Random(Seed);
            mPlayer = new PlayerEntity("Adventurer");
            mEnemy = new EnemyEntity("Monster", mPlayer);
            mPlayer.SetOpponent(mEnemy);
            Debug.LogWarning("//--------------------Game Start--------------------//");
            Debug.Log(mPlayer.ToString());
            Debug.Log(mEnemy.ToString());
            mCount++;
            GameOver.Subscribe(isOver =>
            {
                if (isOver)
                {
                    StartCoroutine(LogGameOver());
                }
            });
        }
        private void Update()
        {
            if (GameOver.Value == false)
            {
                HandleAttack();
                HandleCurative();
                HandleRun();
            }
            HandleReplay();
        }

        private void HandleAttack()
        {
            if (Input.GetKeyDown(KeyCode.Q)) //Attack
            {
                if (mCount > 0)
                {
                    Debug.LogWarningFormat("//------------------第 {0} 回合------------------//", mCount);
                }
                LogCurrentState();
                mPlayer.AddCommand(new AttackCommand(new AttackBev()));
                mPlayer.ExecuteCommand();
                mEnemy.AutoExecuteNextCommand();
                mCount++;
                LogCurrentState();
            }
        }
        private void HandleCurative()
        {
            if (Input.GetKeyDown(KeyCode.W)) //UseCurativePotion
            {
                if (mCount > 0)
                {
                    Debug.LogWarningFormat("//------------------第 {0} 回合------------------//", mCount);
                }
                LogCurrentState();
                mPlayer.AddCommand(new UseCurativePotionCommand(new UseCurativePotionBev()));
                mPlayer.ExecuteCommand();
                mEnemy.AutoExecuteNextCommand();
                mCount++;
                LogCurrentState();
            }
        }
        private void HandleRun()
        {
            if (Input.GetKeyDown(KeyCode.E)) //Runaway
            {
                if (mCount > 0)
                {
                    Debug.LogWarningFormat("//------------------第 {0} 回合------------------//", mCount);
                }
                LogCurrentState();
                mPlayer.AddCommand(new RunawayCommand(new RunawayBev()));
                mPlayer.ExecuteCommand();
                mEnemy.AutoExecuteNextCommand();
                mCount++;
                LogCurrentState();
            }
        }
        private void HandleReplay()
        {
            if (Input.GetKeyDown(KeyCode.Space) && GameOver.Value) //Replay
            {
                StartCoroutine(CommandMemento.Instance.Replay(Consts.REPLAY_WAIT_TIME));
            }
        }
        /// <summary>
        /// 打印当前状态
        /// </summary>
        public void LogCurrentState()
        {
            Debug.Log(mPlayer.Logging());
            Debug.Log(mEnemy.Logging());
        }
        private IEnumerator LogGameOver()
        {
            yield return null;
            Debug.LogWarning("//--------------------Game Over--------------------//");
            Debug.LogWarning("//----------------按 Space 键 Replay----------------//");
        }
        public void DoBeforeReplay()
        {
            Random = new System.Random(Seed);
            mPlayer.Reset();
            mEnemy.Reset();
        }
    }
}